package org.red5.acs.command;

import java.util.ArrayList;
//import java.util.HashMap;
import java.util.Map;

import org.red5.acs.Application;

//import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;
import org.red5.server.api.Red5;
import org.red5.server.api.service.ServiceUtils;
import org.red5.server.api.so.ISharedObject;

public class commandToClient extends Application{
	
	private IScope myIScope = null;
	private Application mainApplication;
	
	public commandToClient(Application _app) {
		mainApplication = _app;
		myIScope = mainApplication.getApp();
	}
	//client	
	public boolean clientLnvoke(String refunctionName,Map<String,Object> re)
	{
		IConnection conn = Red5.getConnectionLocal();
		ServiceUtils.invokeOnConnection(conn, refunctionName, new Object[] {re});
		return true;
	}
	
	public boolean clientLnvoke(String refunctionName,Object[] params)
	{
		IConnection conn = Red5.getConnectionLocal();
		ServiceUtils.invokeOnConnection(conn, refunctionName, params);
		return true;
	}
	
	public boolean clientLnvokeToPoint(IConnection conn,String refunctionName,Map<String,Object> re)
	{
		ServiceUtils.invokeOnConnection(conn, refunctionName, new Object[] {re});
		return true;
	}
	
	public boolean clientLnvokeToPoint(IConnection conn,String refunctionName,Object[] params)
	{
		ServiceUtils.invokeOnConnection(conn, refunctionName, params);
		return true;
	}
	
	//so	
	public boolean soLnvoke(String refunctionName,String _soName,Map<String,Object> re)
	{
		ISharedObject so = getSharedObject(myIScope,_soName);
		//System.out.println("soName:"+_soName+" functionName:"+functionName);
		if(so!= null){
			//System.out.println("so is ok!");
			ArrayList<Object> list = new ArrayList<Object>();
			list.add(re);
			so.sendMessage(refunctionName, list);
		}else{
			//System.out.println("so is null!");
		}
		return true;
	}
	
	public boolean soLnvoke(String refunctionName,String _soName,Object[] params)
	{
		ISharedObject so = getSharedObject(myIScope,_soName);
		if(so!= null){
			System.out.println("so is ok!");
			ArrayList<Object> list = new ArrayList<Object>();
			list.add(params);
			so.sendMessage(refunctionName, list);
		}
		return true;
	}
	
	public boolean allConnectionsLnvoke(String refunctionName,Map<String,Object> re)	{		
		ServiceUtils.invokeOnAllConnections(refunctionName, new Object[] {re});	
		return true;
	}
	
	public boolean allConnectionsLnvoke(String refunctionName,Object[] params){
		ServiceUtils.invokeOnAllConnections(refunctionName, params);	
		return true;
	}
	
}